   
    var is_disabled = true;//倒计时
    var is_captcha  = false;
    //注册表单验证
    function login_validate() {
        let telephone = $.trim($('.loginphone').val());
        let pwd = $.trim($('.loginpwd').val());
        let yzm = $.trim($('#data_code').html());
        let Iptyzm = $('#reg-code').val();
        let phoneReg = /^1[34578]{1}\d{9}$/
        console.log(is_captcha)
        if (!phoneReg.test(telephone)) {
            layer.alert('请输入正确的手机号');
            return false;
        }
        let passwordReg = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/
        if (!passwordReg.test(pwd)) {
            layer.alert('密码必须长度在6-18位');
            return false;
        }
        if(!is_captcha){
            layer.alert('请验证滑块');
            return false;
        }

        return true;
    }
    //忘记密码表单验证
    function forgot_validate(){
        let telephone = $.trim($('.loginphone').val());
        let phoneReg = /^1[34578]{1}\d{9}$/
        console.log(telephone)
        if (!phoneReg.test(telephone)) {
            layer.alert('请输入正确的手机号');
            return false;
        }
        if(!is_captcha){
            layer.alert('请验证滑块');
            return false;
        }
        return true;
    }

    //倒计时
    function time_show() {
        // let button = $('#getcode');
        if (is_disabled) {
            let time = 60;
            timer = setInterval(function () {
                console.log(time);
                if (time <= 0) {
                    $('#getcode').html('重新发送');
                    clearInterval(timer);
                    is_disabled = true;
                } else {
                    is_disabled = false;
                    $('#getcode').html((time) + "秒");
                    time--;
                }
            }, 1000);
        }
    }
    //密码验证
    function passwordValidate() {
        let passwordReg = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/
        if (!passwordReg.test($.trim($('.password').val()))) {
            layer.alert('密码必须长度在6-18位');
            return false;
        }
        if ($.trim($('.password').val()) !== $.trim($('.password2').val())) {
            layer.alert('两次密码不一致');
            return false;
        }
        return true;
    }
    $(function () {
        //获取注册验证码
        $('#register #getcode').click(function () {
            if (login_validate() && is_disabled) {
                $.post('/api/getsmscode', {
                    phone: $.trim($('.loginphone').val()),
                    type: "register",
                }, function (data) {
                    console.log(data)
                    if (data.code == 0) {
                        time_show();
                        //layer.alert('验证码发送成功!');
                        return false;
                    } else {
                        layer.alert(data.msg);
                        return false;
                    }
                }, 'json');
            }
        })
        //进行注册
        $('#register #register-button').click(function () {
            if (login_validate()) {
                let phoneCode = /^\d{4}$/
                if (!phoneCode.test($.trim($('#phoneCode').val()))) {
                    layer.alert('手机验证码有误!');
                    return false;
                }
                $.post('/api/register', {
                    phone: $.trim($('.loginphone').val()),
                    password: $.trim($('.loginpwd').val()),
                    code: $.trim($('#phoneCode').val()),
                }, function (data) {
                    if (data.code == 0) {
                        layer.confirm('注册成功!', {
                            btn: ['去首页逛逛!', '马上去登陆!'] //按钮
                        }, function () {
                            window.location = '/'
                        }, function () {
                            window.location = '/ucenter#/'
                        });
                    } else {
                        layer.alert(data.msg);
                        return false;
                    }
                }, 'json');
            }
        })
        //注册滑块验证
        $("#captcha-slider").slider({
            callback: function(result) {
                if(result){
                    is_captcha = true;
                    $("#code-box").show();
                }
            }
        });

        //进行忘记密码操作
        $('#forget #forget_button').click(function () {
            let phoneCode = /^\d{4}$/
            let vali = forgot_validate();
            if(vali){
                if (!phoneCode.test($.trim($('.phoneCode').val()))) {
                    layer.alert('请输入正确的验证码!');
                    return false;
                } else {
                    $.post('/api/checkforgetcode', {
                        phone: $.trim($('.loginphone').val()),
                        code: $.trim($('.phoneCode').val()),
                    }, function (data) {
                        console.log(data)
                        if (data.code == 0) {
                            $("#reseting").show();
                            $("#forget").hide();
                        } else {
                            layer.alert(data.msg);
                            return false;
                        }
                    }, 'json');
                }
            }
        })
        //进行重置密码操作
        $('#reseting #reseting_button').click(function () {
            if (passwordValidate()) {
                $.post('/api/forgotreset', {
                    phone: $.trim($('.loginphone').val()),
                    password: $.trim($('.password').val()),
                    code: $.trim($('.phoneCode').val()),
                }, function (data) {
                    console.log(data)
                    if (data.code == 0) {
                        layer.confirm('重置密码成功!', {
                            btn: ['去首页逛逛!', '马上去登陆!'] //按钮
                        }, function () {
                            window.location = '/'
                        }, function () {
                            window.location = '/login'
                        });
                    } else {
                        layer.alert(data.msg);
                        $("#forget").show();
                        $("#reseting").hide();
                    }
                }, 'json');
            }
        })
        //获取忘记密码验证码
        $('#forget .code').click(function () {
            if (forgot_validate() && is_disabled) {
                $.post('/api/getsmscode', {
                    phone: $.trim($('.loginphone').val()),
                    type: "forgot",
                }, function (data) {
                    console.log(data)
                    if (data.code == 0) {
                        time_show();
                        //layer.alert('验证码发送成功!');
                        return false;
                    } else {
                        layer.alert(data.msg);
                        return false;
                    }
                }, 'json');
            }
        })
    })